﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace CrawlFish.Parsing
{
    public class HtmlParser
    {
        public HtmlParser(string markup)
        {
            _content = markup;
            _markup = markup;
            _stripHead();
            _stripComments();
            _stripTags();
            _stripSymbols();
            _findLinks();
        }

        public string GetContent()
        {
            Regex reg = new Regex("\\s+");
            _content = reg.Replace(_content, " ");
            return _content;
        }

        public string GetMarkup()
        {
            Regex reg = new Regex("\\s+");
            _markup = reg.Replace(_markup, " ");
            return _markup;
        }

        public List<string> GetLinks()
        {
            return _links;
        }

        private void _findLinks()
        {
            foreach (string tag in _tags)
            {
                if (tag.StartsWith("<a"))
                {
                    int index = tag.IndexOf("href=\"");
                    if (index > 0)
                    {
                        index += 6;

                        int end = tag.IndexOf("\"", index);
                        string href = tag.Substring(index, (end - index));
                        if (href.StartsWith("http://"))
                        {
                            _links.Add(href);
                        }
                    }
                }
            }
        }


        private void _stripHead()
        {
            _strip("<head>", "</head>", _tags);
        }

        private void _stripTags()
        {
            _strip("<", ">", _tags);
        }

        private void _stripComments()
        {
            _strip("<!--", "-->", _comments);
        }

        private void _stripSymbols()
        {
            _strip("&", ";", _comments);
        }

        private void _strip(string startTag, string endTag, List<string> collection)
        {
            int offset = 0;
            int length = _content.Length;
            while (offset >= 0 && offset <= length)
            {
                int index = _content.IndexOf(startTag, offset, StringComparison.CurrentCultureIgnoreCase);
                if (index >= 0 && index >= offset)
                {
                    int end = _content.IndexOf(endTag, index, StringComparison.CurrentCultureIgnoreCase);
                    string block = _content.Substring(index, (end - index + endTag.Length));
                    collection.Add(block);
                    offset = end;
                }
                else
                {
                    offset = index;
                }
            }

            foreach (string block in collection)
            {
                _content = _content.Replace(block, "");
            }
        }

        private string _content;
        private string _markup;
        private List<string> _comments = new List<string>();
        private List<string> _tags = new List<string>();
        private List<string> _links = new List<string>();
    }
}
